﻿using System;
using System.Data;
using Ext.Net;
using BLL;

namespace ERP.Modules
{
    public partial class Test : System.Web.UI.UserControl
    {
        private readonly Bank_BLL clsBank = new Bank_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                GetData();
                ClearTextBox();
            }
        }
        //-------Store-Datasource-----------------
        protected void StoreBank_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetData();
        }
        //--------Buttom-Main------------------------
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            WindowsBank.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grBank.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    ClearTextBox();
                    DataDetail(int.Parse(row.RecordID));
                    WindowsBank.Show();
                    sm.UpdateSelection();
                }
        }

        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grBank.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    try
                    {
                        Bank_BLL.Deletetb_Bank(int.Parse(row.RecordID));
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetData();
        }
        //--------Buttom-Popup------------------------
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtBank_ID.Text == string.Empty)
            {
                InsertBank();
            }
            else
            {
                UpdateBank();
            }
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsBank.Close();
        }
        //----------Click-------------------
        protected void Cell_Click(object sender, DirectEventArgs e)
        {

        }
        protected void BankDblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int Bank_Id = int.Parse(e.ExtraParams["Bank_Id"]);
            DataDetail(Bank_Id);
            WindowsBank.Show();
        }
        //----------Button-Windows---------------
        #region[Method]
        protected void GetData()
        {
            DataTable dtBank = clsBank.BindGirdtb_Bank(string.Empty);
            if (dtBank != null && dtBank.Rows.Count > 0)
            {
                StoreBank.DataSource = dtBank;
                StoreBank.DataBind();
            }
        }
        protected void DataDetail(int id)
        {
            DataTable dtBank = clsBank.BindGirdtb_BankDetail(id);
            if (dtBank != null && dtBank.Rows.Count > 0)
            {
                txtBank_ID.Text = dtBank.Rows[0]["Bank_ID"].ToString();
                txtBank_Alias.Text = dtBank.Rows[0]["Bank_Alias"].ToString();
                txtBank_Name.Text = dtBank.Rows[0]["Bank_Name"].ToString();
                txtBank_Note.Text = dtBank.Rows[0]["Bank_Note"].ToString();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var bank = new Bank_BLL();
            var dtBank = bank.BindGirdtb_Bank(string.Empty);
            if (dtBank != null && dtBank.Rows.Count > 0)
            {
                foreach (DataRow dr in dtBank.Rows)
                {
                    if (txtBank_ID.Text != dr["Bank_ID"].ToString())
                    {
                        if (txtBank_Alias.Text == dr["Bank_Alias"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã ngân hàng đã tồn tại !");
                        }
                        else if (txtBank_Name.Text == dr["Bank_Name"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tên ngân hàng đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertBank()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new ET.Bank_ET
                    {
                        Bank_Alias = txtBank_Alias.Text,
                        Bank_Name = txtBank_Name.Text,
                        Bank_Note = txtBank_Note.Text,
                        Bank_Date_Add = DateTime.Now,
                        Bank_Computer_Add = Environment.MachineName
                    };
                    Bank_BLL.Inserttb_Bank(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetData();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        protected void UpdateBank()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new ET.Bank_ET
                    {
                        Bank_Id = int.Parse(txtBank_ID.Text),
                        Bank_Alias = txtBank_Alias.Text,
                        Bank_Name = txtBank_Name.Text,
                        Bank_Note = txtBank_Note.Text,
                        Bank_Date_Modify = DateTime.Now,
                        Bank_Computer_Modify = Environment.MachineName
                    };
                    Bank_BLL.Updatetb_Bank(obj);
                    WindowsBank.Close();
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Sữa thành công!");
                    GetData();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            FormPanelBank.Reset();

            txtBank_ID.Text = string.Empty;
            txtBank_Alias.Text = MaTuDong();
            txtBank_Name.Text = string.Empty;
            txtBank_Note.Text = string.Empty;
        }
        #endregion
        #region[MaTangTuDong]
        protected string MaTuDong()
        {
            int i;
            DataTable dt = clsBank.BindGirdtb_Bank(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Bank_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Bank_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Bank_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "NH_00" + i;
        }
        #endregion
        #region[Search]
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            DataTable dtBank = clsBank.BindGirdtb_Bank(txtSearch.Text);
            if (dtBank != null && dtBank.Rows.Count > 0)
            {
                StoreBank.DataSource = dtBank;
                StoreBank.DataBind();
            }
        }
        #endregion
    }
}